<?php

require_once('backend.php');

class Admin extends Backend {

    //put your code here
    function Admin() {
        parent::Backend();
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->helper('url');
        $this->load->helper('text');
        $this->load->model('Mcategory');
    }

    function index($link = NULL, $view = NULL, $action = NULL, $dt_action = NULL) {
        switch ($link) {
            case 'home':
                $this->data['breadcrumb'] = $this->createBreadcrunb("Trang chủ/Dashboard", "home/dashboard");
                $this->meta[AppContants::META_TITLE] = 'Welcome admin CMS Admin | CMS';
                $this->layout[AppContants::LAYOUT_MAINVIEW] = $this->template . '/home';
                break;

            case 'products':
                if ($view == NULL) {
                    $this->data['breadcrumb'] = $this->createBreadcrunb("Trang chủ/Danh sách sản phẩm", "home/dashboard");
                    $this->meta[AppContants::META_TITLE] = 'Welcome admin CMS Admin | CMS';
                    $this->layout[AppContants::LAYOUT_MAINVIEW] = $this->template . '/product-list';
//                    $this->getMenu($menu, 0, 1, '', $newstr);
//                    $this->data['category_list'] = $newstr;
                } else {
                    if ($view == 'show') {
                        $this->updateStatus($action, 'Mcategory', 'inactive');
                        redirect('admin/' . $link);
                    } else if ($view == 'delete') {
                        $this->updateStatus($action, 'Mcategory', 'delete');
                        redirect('admin/' . $link);
                    } elseif ($view == 'add') {
                        $this->data['breadcrumb'] = $this->createBreadcrunb("Trang chủ/Danh sách sản phẩm/Thêm mới sản phẩm", "home/users/");
                        $this->layout[AppContants::LAYOUT_MAINVIEW] = $this->template . '/product-add';
                        $this->meta[AppContants::META_TITLE] = 'Thêm mới sản phẩm | CMS';

//                    //get list for select tag
//                    $this->recursive($menu, 0, 1, '--', $newstr);
//                    $this->data['select_menu'] = $newstr;
//
//                    //config view page
//                    $this->data['url_submit'] = base_url('admin/category-list/add/submit');
//                    $this->data['title_page'] = 'Thêm mới Menu Website';
                        //Submit add new
                        if ($action == 'submit') {
                            $friendly_url = strtolower(url_title(removeSign($this->input->post('menu_alias'))));
                            $data = array(
                                'parent_id' => $this->input->post('menu_parent'),
                                'friendly_url' => $friendly_url,
                                'name' => $this->input->post('menu_name'),
                                'note' => $this->input->post('menu_detail'),
                                'in_order' => $this->input->post('menu_orderby'),
                                'status' => $this->input->post('menu_status'),
                                'created_by' => 'admin',
                            );

                            $this->Mcategory->addNew($data);
                            redirect('admin/' . $link);
                        }
                    } else {
                        $this->data['breadcrumb'] = $this->createBreadcrunb("Trang chủ/Danh mục menu website/Cập nhật Menu Website", "home/category-list/");
                        $this->layout[AppContants::LAYOUT_MAINVIEW] = $this->template . '/category-add';
                        $this->meta[AppContants::META_TITLE] = 'Cập nhật Menu Website | CMS';

                        //get category detail
                        $category_detail = $this->Mcategory->getCategoryDetailByIdADMIN($action);
                        $this->data['category_detail'] = $category_detail;

                        //get list for select tag
                        $this->recursive($menu, 0, 1, '--', $newstr);
                        $this->data['select_menu'] = $newstr;

                        //config view page
                        $this->data['url_submit'] = base_url('admin/category-list/edit/' . $action . '/submit');
                        $this->data['title_page'] = 'Cập nhật Menu Website';


                        //Submit edit
                        if ($dt_action == 'submit') {
                            $friendly_url = strtolower(url_title(removeSign($this->input->post('menu_alias'))));
                            $data = array(
                                'parent_id' => $this->input->post('menu_parent'),
                                'friendly_url' => $friendly_url,
                                'name' => $this->input->post('menu_name'),
                                'note' => $this->input->post('menu_detail'),
                                'in_order' => $this->input->post('menu_orderby'),
                                'status' => $this->input->post('menu_status'),
                                'updated_by' => 'admin',
                            );

                            $this->Mcategory->update($action, $data);
                            redirect('admin/' . $link);
                        }
                    }
                }

                break;

            case 'articles':

                if ($view == NULL) {
                    $this->data['breadcrumb'] = $this->createBreadcrunb("Trang chủ/Danh sách bài viết", "home/dashboard");
                    $this->meta[AppContants::META_TITLE] = 'Welcome admin CMS Admin | CMS';
                    $this->layout[AppContants::LAYOUT_MAINVIEW] = $this->template . '/article-list';
//                    $this->getMenu($menu, 0, 1, '', $newstr);
//                    $this->data['category_list'] = $newstr;
                } else {
                    if ($view == 'show') {
                        $this->updateStatus($action, 'Mcategory', 'inactive');
                        redirect('admin/' . $link);
                    } else if ($view == 'delete') {
                        $this->updateStatus($action, 'Mcategory', 'delete');
                        redirect('admin/' . $link);
                    } elseif ($view == 'add') {
                        $this->data['breadcrumb'] = $this->createBreadcrunb("Trang chủ/Danh sách nhân viên/Thêm mới nhân viên", "home/users/");
                        $this->layout[AppContants::LAYOUT_MAINVIEW] = $this->template . '/article-detail';
                        $this->meta[AppContants::META_TITLE] = 'Thêm mới bài viết | CMS';

//                    //get list for select tag
//                    $this->recursive($menu, 0, 1, '--', $newstr);
//                    $this->data['select_menu'] = $newstr;
//
//                    //config view page
//                    $this->data['url_submit'] = base_url('admin/category-list/add/submit');
//                    $this->data['title_page'] = 'Thêm mới Menu Website';
                        //Submit add new
                        if ($action == 'submit') {
                            $friendly_url = strtolower(url_title(removeSign($this->input->post('menu_alias'))));
                            $data = array(
                                'parent_id' => $this->input->post('menu_parent'),
                                'friendly_url' => $friendly_url,
                                'name' => $this->input->post('menu_name'),
                                'note' => $this->input->post('menu_detail'),
                                'in_order' => $this->input->post('menu_orderby'),
                                'status' => $this->input->post('menu_status'),
                                'created_by' => 'admin',
                            );

                            $this->Mcategory->addNew($data);
                            redirect('admin/' . $link);
                        }
                    } else {
                        $this->data['breadcrumb'] = $this->createBreadcrunb("Trang chủ/Danh mục menu website/Cập nhật Menu Website", "home/category-list/");
                        $this->layout[AppContants::LAYOUT_MAINVIEW] = $this->template . '/category-add';
                        $this->meta[AppContants::META_TITLE] = 'Cập nhật Menu Website | CMS';

                        //get category detail
                        $category_detail = $this->Mcategory->getCategoryDetailByIdADMIN($action);
                        $this->data['category_detail'] = $category_detail;

                        //get list for select tag
                        $this->recursive($menu, 0, 1, '--', $newstr);
                        $this->data['select_menu'] = $newstr;

                        //config view page
                        $this->data['url_submit'] = base_url('admin/category-list/edit/' . $action . '/submit');
                        $this->data['title_page'] = 'Cập nhật Menu Website';


                        //Submit edit
                        if ($dt_action == 'submit') {
                            $friendly_url = strtolower(url_title(removeSign($this->input->post('menu_alias'))));
                            $data = array(
                                'parent_id' => $this->input->post('menu_parent'),
                                'friendly_url' => $friendly_url,
                                'name' => $this->input->post('menu_name'),
                                'note' => $this->input->post('menu_detail'),
                                'in_order' => $this->input->post('menu_orderby'),
                                'status' => $this->input->post('menu_status'),
                                'updated_by' => 'admin',
                            );

                            $this->Mcategory->update($action, $data);
                            redirect('admin/' . $link);
                        }
                    }
                }

                break;

            case 'users':

                if ($view == NULL) {
                    $this->data['breadcrumb'] = $this->createBreadcrunb("Trang chủ/Danh sách nhân viên", "home/dashboard");
                    $this->meta[AppContants::META_TITLE] = 'Welcome admin CMS Admin | CMS';
                    $this->layout[AppContants::LAYOUT_MAINVIEW] = $this->template . '/users-list';
//                    $this->getMenu($menu, 0, 1, '', $newstr);
//                    $this->data['category_list'] = $newstr;
                } else {
                    if ($view == 'show') {
                        $this->updateStatus($action, 'Mcategory', 'inactive');
                        redirect('admin/' . $link);
                    } else if ($view == 'delete') {
                        $this->updateStatus($action, 'Mcategory', 'delete');
                        redirect('admin/' . $link);
                    } elseif ($view == 'add') {
                        $this->data['breadcrumb'] = $this->createBreadcrunb("Trang chủ/Danh sách nhân viên/Thêm mới nhân viên", "home/users/");
                        $this->layout[AppContants::LAYOUT_MAINVIEW] = $this->template . '/user-edit';
                        $this->meta[AppContants::META_TITLE] = 'Thêm mới nhân viên | CMS';

//                    //get list for select tag
//                    $this->recursive($menu, 0, 1, '--', $newstr);
//                    $this->data['select_menu'] = $newstr;
//
//                    //config view page
//                    $this->data['url_submit'] = base_url('admin/category-list/add/submit');
//                    $this->data['title_page'] = 'Thêm mới Menu Website';
                        //Submit add new
                        if ($action == 'submit') {
                            $friendly_url = strtolower(url_title(removeSign($this->input->post('menu_alias'))));
                            $data = array(
                                'parent_id' => $this->input->post('menu_parent'),
                                'friendly_url' => $friendly_url,
                                'name' => $this->input->post('menu_name'),
                                'note' => $this->input->post('menu_detail'),
                                'in_order' => $this->input->post('menu_orderby'),
                                'status' => $this->input->post('menu_status'),
                                'created_by' => 'admin',
                            );

                            $this->Mcategory->addNew($data);
                            redirect('admin/' . $link);
                        }
                    } else {
                        $this->data['breadcrumb'] = $this->createBreadcrunb("Trang chủ/Danh mục menu website/Cập nhật Menu Website", "home/category-list/");
                        $this->layout[AppContants::LAYOUT_MAINVIEW] = $this->template . '/category-add';
                        $this->meta[AppContants::META_TITLE] = 'Cập nhật Menu Website | CMS';

                        //get category detail
                        $category_detail = $this->Mcategory->getCategoryDetailByIdADMIN($action);
                        $this->data['category_detail'] = $category_detail;

                        //get list for select tag
                        $this->recursive($menu, 0, 1, '--', $newstr);
                        $this->data['select_menu'] = $newstr;

                        //config view page
                        $this->data['url_submit'] = base_url('admin/category-list/edit/' . $action . '/submit');
                        $this->data['title_page'] = 'Cập nhật Menu Website';


                        //Submit edit
                        if ($dt_action == 'submit') {
                            $friendly_url = strtolower(url_title(removeSign($this->input->post('menu_alias'))));
                            $data = array(
                                'parent_id' => $this->input->post('menu_parent'),
                                'friendly_url' => $friendly_url,
                                'name' => $this->input->post('menu_name'),
                                'note' => $this->input->post('menu_detail'),
                                'in_order' => $this->input->post('menu_orderby'),
                                'status' => $this->input->post('menu_status'),
                                'updated_by' => 'admin',
                            );

                            $this->Mcategory->update($action, $data);
                            redirect('admin/' . $link);
                        }
                    }
                }

                break;
            case 'category-list':
                $menu = $this->Mcategory->getAllCategoryADMIN();
                if ($view == NULL) {
                    $this->data['breadcrumb'] = $this->createBreadcrunb("Trang chủ/Danh mục menu website", "home/category-list");
                    $this->meta[AppContants::META_TITLE] = 'Danh sách menu website | CMS';
                    $this->getMenu($menu, 0, 1, '', $newstr);
                    $this->data['category_list'] = $newstr;
                    $this->layout[AppContants::LAYOUT_MAINVIEW] = $this->template . '/' . $link;
                } else {
                    if ($view == 'show') {
                        $this->updateStatus($action, 'Mcategory', 'inactive');
                        redirect('admin/' . $link);
                    } else if ($view == 'delete') {
                        $this->updateStatus($action, 'Mcategory', 'delete');
                        redirect('admin/' . $link);
                    } elseif ($view == 'add') {
                        $this->data['breadcrumb'] = $this->createBreadcrunb("Trang chủ/Danh mục menu website/Thêm mới Menu Website", "home/category-list/");
                        $this->layout[AppContants::LAYOUT_MAINVIEW] = $this->template . '/category-add';
                        $this->meta[AppContants::META_TITLE] = 'Thêm mới Menu Website | CMS';

                        //get list for select tag
                        $this->recursive($menu, 0, 1, '--', $newstr);
                        $this->data['select_menu'] = $newstr;

                        //config view page
                        $this->data['url_submit'] = base_url('admin/category-list/add/submit');
                        $this->data['title_page'] = 'Thêm mới Menu Website';


                        //Submit add new
                        if ($action == 'submit') {
                            $friendly_url = strtolower(url_title(removeSign($this->input->post('menu_alias'))));
                            $data = array(
                                'parent_id' => $this->input->post('menu_parent'),
                                'friendly_url' => $friendly_url,
                                'name' => $this->input->post('menu_name'),
                                'note' => $this->input->post('menu_detail'),
                                'in_order' => $this->input->post('menu_orderby'),
                                'status' => $this->input->post('menu_status'),
                                'created_by' => 'admin',
                            );

                            $this->Mcategory->addNew($data);
                            redirect('admin/' . $link);
                        }
                    } else {
                        $this->data['breadcrumb'] = $this->createBreadcrunb("Trang chủ/Danh mục menu website/Cập nhật Menu Website", "home/category-list/");
                        $this->layout[AppContants::LAYOUT_MAINVIEW] = $this->template . '/category-add';
                        $this->meta[AppContants::META_TITLE] = 'Cập nhật Menu Website | CMS';

                        //get category detail
                        $category_detail = $this->Mcategory->getCategoryDetailByIdADMIN($action);
                        $this->data['category_detail'] = $category_detail;

                        //get list for select tag
                        $this->recursive($menu, 0, 1, '--', $newstr);
                        $this->data['select_menu'] = $newstr;

                        //config view page
                        $this->data['url_submit'] = base_url('admin/category-list/edit/' . $action . '/submit');
                        $this->data['title_page'] = 'Cập nhật Menu Website';


                        //Submit edit
                        if ($dt_action == 'submit') {
                            $friendly_url = strtolower(url_title(removeSign($this->input->post('menu_alias'))));
                            $data = array(
                                'parent_id' => $this->input->post('menu_parent'),
                                'friendly_url' => $friendly_url,
                                'name' => $this->input->post('menu_name'),
                                'note' => $this->input->post('menu_detail'),
                                'in_order' => $this->input->post('menu_orderby'),
                                'status' => $this->input->post('menu_status'),
                                'updated_by' => 'admin',
                            );

                            $this->Mcategory->update($action, $data);
                            redirect('admin/' . $link);
                        }
                    }
                }
                break;

            default :
                break;
        }
    }

}
?>
